<!DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>最新技术成果</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation_tech" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form " action="" lay-filter="deptInfo" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>


            <div class="layui-form-item">
                <label class="layui-form-label">关键字</label>
                <div class="layui-input-block">
                    <input type="name" name="keywords" placeholder="请输入关键字" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <div class="layui-form-label ">技术描述</div>
                <div class="layui-input-block">
                    <input type="description" name="description" placeholder="请输入技术简要描述" autocomplete="off" class="layui-input" >
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="status" lay-skin="switch" lay-filter="switch" lay-text="启用|禁用" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">提交技术</button>
                    <button  class="layui-btn layui-btn-primary" id="btn_cancel">取消</button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="tech_table_div">
    <div id="searchParam" shiro:hasPermission="sys:tech:list">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="techId" class="layui-input"  autocomplete="off" placeholder="请输入技术ID">
            </div>
            <div class="layui-input-inline">
                <input type="text" id="techkeyWords" class="layui-input"  autocomplete="off" placeholder="请输入关键字">
            </div>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="createTime" placeholder="创建时间">
            </div>
            <div class="layui-input-inline layui-form">
                <select  id="status"  >
                    <option value="">请选择技术开启状态</option>
                    <option value="1">正常</option>
                    <option value="0">弃用</option>
                </select>
            </div>
            <div class="layui-input-inline ">
                <button class="layui-btn"   id="search">查询</button>
            </div>
        </div>

    </div>
    <table class="layui-hide" id="tech_table" lay-filter="tech_table"></table>
    <div id="laypage" class="layui-laypage-btn"></div>
</div>
</body>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button type="button" class="layui-btn" lay-event="addNewTech" shiro:hasPermission="sys:tech:add">
            <i class="layui-icon">&#xe608;</i> 新增技术
        </button>
    </div>
</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="sys:tech:update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="sys:tech:delete">删除</a>
</script>
<script src="/layui/layui.js"></script>
<script src="/js/core.util.js"></script>
<script>
    layui.use(['jquery','form','table','laypage', 'layer','laydate'],function () {
        var table = layui.table;
        var laypage = layui.laypage
        var form = layui.form;
        var layer = layui.layer;
        var $ =layui.jquery;
        var laydate=layui.laydate;
        var searchParam= {
            pageNum:1,
            pageSize:10,
            techId:null,
            techkeyWords:null,
            status:null,
            startTime:null,
            endTime:null

        }
        /*动态获取数据*/
        CoreUtil.sendAjax("/api/techs",JSON.stringify(searchParam),function (res) {
            //初始化分页器
            laypageTable(res.data.totalRows,searchParam.pageNum);
            //初始化渲染数据
            if(res.data.list !=null){
                loadTable(res.data.list);
            }
        },"POST",false,function (res) {
            //无权限访问最新技术鑫鑫提示
            layer.msg("抱歉！您暂无获取最新技术鑫鑫的权限");
            var noAuthorityData=[];
            loadTable(noAuthorityData);
        });
        //渲染分页插件
        var laypageTable = function(count,currentPage) {
            laypage.render({
                elem: 'laypage'
                , count: count
                ,limit:searchParam.pageSize
                , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                ,curr: location.hash.replace('#!currentPage=', '') //获取起始页
                ,hash: 'currentPage' //自定义hash值
                , jump: function (obj,first) {
                    if (!first){
                        searchParam.pageNum=obj.curr;
                        searchParam.pageSize=obj.limit;
                        CoreUtil.sendAjax("/api/techs",JSON.stringify(searchParam),function (res) {
                            if(res.data.list !=null){
                                loadTable(res.data.list);
                                laypageTable(res.data.totalRows,searchParam.pageNum);
                            }

                        },"POST",false,function (res) {
                            //无权限访问最新技术鑫鑫提示
                            layer.msg("抱歉！您暂无获取最新技术鑫鑫的权限");
                            var noAuthorityData=[];
                            loadTable(noAuthorityData);
                        });
                    }
                }
            });
        };
        //渲染table
        var loadTable=function (data) {
            //展示已知数据
            table.render({
                elem: '#tech_table'
                ,cols: [
                    [
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'id', title: '技术ID', width: 200, sort: true},
                        {field: 'keywords', title: '技术关键字', width: 100},
                        {field: 'description', title: '技术描述', width: 300},
                        {
                            field: 'createTime', title: '创建时间', minWidth: 120 , templet: function (item) {
                                return CoreUtil.formattime(item.createTime);
                            }
                        },
                        {
                            field: 'updateTime', title: '更新时间', minWidth: 120,templet: function (item) {
                                return CoreUtil.formattime(item.updateTime);
                            }
                        },
                        {
                            field: 'status', title: '状态', width: 100,templet: function (item) {
                                if(item.status === 1){
                                    return  '  <input type="checkbox" lay-skin="switch" lay-text="正常|弃用" checked disabled>';
                                }
                                if(item.status === 0){
                                    return  '  <input type="checkbox" lay-skin="switch" lay-text="正常|弃用" disabled>';
                                }
                            }
                        },
                        {title:'操作',width:250,toolbar:'#tool'}
                    ]
                ]
                ,data: data
                ,even: true
                ,limit: data.length
                ,limits: [10, 20, 30, 40, 50]
                ,toolbar: '#toolbar'
            });
        };


        //新增新技术事件
        table.on('toolbar(tech_table)',function (obj) {

            switch (obj.event) {
                case 'addNewTech':
                    //隐藏表格
                    $(".tech_table_div").hide();
                    //显示编辑窗口
                    $(".operation_tech").show();
                    $(".title").html("新增技术");
                    $(".operation_tech input[name=id]").val("");
                    $(".operation_tech input[name=keywords]").val("");
                    $(".operation_tech input[name=description]").val("");
                    $(".operation_tech input[name=status]").attr('checked', 'checked');
                    $(".operation_tech input[name=status]").attr('type', 'hidden').val(1);
                    var x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    x[0].setAttribute("class", "layui-unselect layui-form-switch layui-form-onswitch");
                    var d = document.getElementsByTagName('em')[0];
                    d.firstChild.nodeValue = '启用';
                    form.render(); //更新全部
                    break;
            }
        });

        //监听指定开关
        form.on('switch(switch)', function(){
            $(".operation_tech input[name=status]").attr('type', 'hidden').val(this.checked ? 1 : 0);

        });

        //编辑或新增的界面取消事件
        $("#btn_cancel").click(function() {
            //显示表格
            $(".tech_table_div").show();
            //隐藏编辑窗口
            $(".operation_tech").hide();
            //可以防止在表单事件操作后自动刷新
            return false;
        });

        //监听提交
        form.on('submit(submit)', function(data){
            var params={
                id:data.field.id,
                keywords:data.field.keywords,
                description:data.field.description,
                status:data.field.status,
            }
            //新增
            if(data.field.id===undefined || data.field.id===null || data.field.id===""){
                CoreUtil.sendAjax("/api/tech",JSON.stringify(params),function (res) {
                    //显示表格
                    $(".tech_table_div").show();
                    //隐藏编辑窗口
                    $(".operation_tech").hide();
                    $(".layui-laypage-btn").click();
                },"POST",false,function (res) {
                    //无权限新增角色提示
                    layer.msg("抱歉！您暂无新增角色信息详情的权限");
                });
            }else {
                CoreUtil.sendAjax("/api/tech",JSON.stringify(params),function (res) {
                    //显示表格
                    $(".tech_table_div").show();
                    //隐藏编辑窗口
                    $(".operation_tech").hide();
                    $(".layui-laypage-btn").click();
                },"PUT",false,function (res) {
                    //无权限更新角色提示
                    layer.msg("抱歉！您暂无更新角色信息详情的权限");
                });
            }

            return false;
        });


        //日期范围
        laydate.render({
            elem: '#createTime'
            ,type: 'datetime'
            ,range: '~'
            ,done: function(value, date, endDate){
                if(value !=null && value != undefined && value != ""){
                    searchParam.startTime=value.split("~")[0];
                    searchParam.endTime=value.split("~")[1];
                }else {
                    //清空时间的时候要清空以前选择的日期
                    searchParam.startTime=null;
                    searchParam.endTime=null;
                }

            }
        });


        //执行查询
        $("#search").click(function () {
            searchParam.techId=$("#techId").val();
            searchParam.status=$("#status").val();
            searchParam.techkeyWords=$("#techkeyWords").val();
            searchParam.pageNum=1;
            CoreUtil.sendAjax("/api/techs",JSON.stringify(searchParam),function (res) {
                //初始化分页器
                laypageTable(res.data.totalRows,searchParam.pageNum);
                //初始化渲染数据
                if(res.data.list !=null){
                    loadTable(res.data.list);
                }
            },"POST",false,function (res) {
                //无权限访问最新技术鑫鑫提示
                layer.msg("抱歉！您暂无获取最新技术鑫鑫的权限");
                var noAuthorityData=[];
                loadTable(noAuthorityData);
            });
        });
        //操作项事件
        table.on('tool(tech_table)',function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    //隐藏表格
                    $(".tech_table_div").hide();
                    //显示编辑窗口
                    $(".operation_tech").show();
                    $(".title").html("编辑技术");
                    // layer.msg('你选择了'+data.name);
                    getTechDetail(data.id);
                    break;
                case 'del':
                    // layer.msg("确定删除"+data.name);
                    tipDialog(data.id,data.keywords);
                    break;
            }
        });

        //获取技术详情
        var getTechDetail=function(id) {
            CoreUtil.sendAjax("/api/tech/" + id, null, function (res) {
                $(".operation_tech input[name=id]").val(res.data.id);
                $(".operation_tech input[name=keywords]").val(res.data.keywords);
                $(".operation_tech input[name=description]").val(res.data.description);
                if (res.data.status == 1) {
                    $(".operation_tech input[name=status]").attr('checked', 'checked');
                    $(".operation_tech input[name=status]").attr('type', 'hidden').val(1);
                    var x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    x[0].setAttribute("class", "layui-unselect layui-form-switch layui-form-onswitch");
                    var d = document.getElementsByTagName('em')[0];
                    d.firstChild.nodeValue = '启用';
                } else {
                    $(".operation_tech input[name=status]").attr('type', 'hidden').removeAttr("checked").val(0);
                    var x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    x[0].setAttribute("class", "layui-unselect layui-form-switch");
                    var d = document.getElementsByTagName('em')[0];
                    d.firstChild.nodeValue = '禁用';
                }
                form.render(); //更新全部
            }, "GET", false,function (res) {
                //无权限访问角色详情提示
                layer.msg("抱歉！您暂无获取角色信息详情的权限");
            });
        }

        //删除前确认对话框
        var tipDialog=function (id,keywords) {
            layer.open({
                content: "确定要删除 "+keywords+" 技术么？",
                yes: function(index, layero){
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendAjax("/api/tech/"+id,null,function (res) {
                        layer.msg(res.msg);
                        $(".layui-laypage-btn").click();
                    },"DELETE",false,function (res) {
                        //无权限删除角色提示
                        layer.msg("抱歉！您暂无删除角色信息详情的权限");
                    });
                }
            });
        }

    });
</script>
</html>